// AntColony.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Environment.h"
#include "Ant.h"
#include "ItemCreator.h"
#include "AntColony.h"
#include <ctime>
#include "FoodCountException.h"
#include <vector>

int _tmain(int argc, _TCHAR* argv[])
{
	std::srand(std::time(0));

	CEnvironment *TestEnvironment = CEnvironment::Instance(); // Creating Environment

	CItemCreator *CreateItem = CItemCreator::Instance(); // Creating ItemCreator

	TestEnvironment->Init(SIZE_HORIZONTAL,SIZE_VERTICAL); // Initializing the TestEnvironment with the fields
	//std::cout<<"Environment\n";

	CItem *pAnthil=CreateItem->CreateAntHill(g_uiAntHillInitalFoodCount);
	TestEnvironment->InsertItem(5,9,pAnthil);
	//TestEnvironment->InsertItem(2,9,CreateItem->CreateFood());
	//TestEnvironment->InsertItem(9,3,CreateItem->CreateFood());
	std::vector<std::pair<int,int>> vecpairCreated;
	vecpairCreated.push_back(std::make_pair(5,9));
	for (unsigned int uiIndex=0;uiIndex<g_uiFoodPlaceCount;++uiIndex)
	{
		auto pair=std::make_pair((std::rand())%SIZE_HORIZONTAL,(std::rand())%SIZE_VERTICAL);
		bool bFound=false;
		for (auto item : vecpairCreated)
		{
			if (item.first==pair.first && item.second==pair.second)
			{
				bFound=true;
			}
		}
		if (!bFound)
		{
			vecpairCreated.push_back(pair);
			TestEnvironment->InsertItem(pair.first,pair.second,CreateItem->CreateFood());
		}
	}
	//std::cout<<"Insert of an Anthill at position 9,5\n";

	TestEnvironment->PrintEnvironment(); // Print of the Environment for debugging
	//std::cout<<"\n\nPointer count for each area\n";
	TestEnvironment->PrintPointerCount(); // Counting the references of each field to the neighbour fields

	//std::cout<<"\n\nCount number of items for each area\n";
	TestEnvironment->PrintNumberOfItems(); // Counting the references of each field to the neighbour fields
	
	
	try{

		for(int i=0 ;i<300000; i++){
			TestEnvironment->actAll( i );
			TestEnvironment->PrintNumberOfItems();
			_sleep(500);
			//system("PAUSE");
		}
	}

		catch(CFoodCountException& e){
			std::cout << e.what() << e.getFoodCount() << std::endl;
		}

		catch(...) {
			std::cout << "Problem occoured! Program stop" << std::endl;
		}

	/*Test of the ItemCreator Class
	CItem* pAmeise = CreateItem->CreateAnt();
	CItem* pFood = CreateItem->CreateFood();
	CItem* pAnthill = CreateItem->CreateAntHill();

	for(int i=0 ;i<10; i++){
		pAmeise->act();
	}
	pFood->act();
	pAnthill->act();
	*/
	system("PAUSE");
	TestEnvironment->Clean();
	

	return 0;
}

